package com.sytc.camera.util;

import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.File;

public class Util {

    private static final Logger log = LoggerFactory.getLogger(Util.class);

    // 封装成一个方法
    public static String getCurrentTimestamp() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    // 获取当前时间戳，格式：yyyyMMdd_HHmmss_SSS
    public static String getMsTimestamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
        return sdf.format(new Date());
    }

    public static void appendTextArea(TextArea showMessage, String message) {

        // 在主线程中更新 UI
        Platform.runLater(() -> {
            // 在 TextArea 中显示消息和时间戳
            showMessage.appendText("[" + getCurrentTimestamp() + "] " + message + "\n"); // 调用现有的 packMessage 方法
            // 自动滚动到 TextArea 的最后一行
            showMessage.setScrollTop(Double.MAX_VALUE); // 滚动到最后一行
        });
    }


    public static String getAbsolutePath(String fileName) {
        // 获取资源文件的 URL
        URL resource = Util.class.getClassLoader().getResource(fileName);

        if (resource != null) {
            // 将 URL 转换为文件路径
            return (new File(resource.getFile())).getAbsolutePath();
        } else {
            log.info("文件未找到: " + fileName);
            return null;
        }
    }


    // 错误弹窗提示
    public static void errorAlert(String message) {
        log.error(message);
        Platform.runLater(() -> {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText(message);
            alert.setContentText(null);
            alert.showAndWait();
        });
    }

    public static int indexOf(byte[] array, byte valueToFind, int startIndex, int endIndex) {
        if (array == null) {
            return -1;
        } else {
            if (startIndex < 0) {
                startIndex = 0;
            }
            if(endIndex <= startIndex){
                return -1;
            }
            if(endIndex > array.length || endIndex < 0){
                endIndex = array.length;
            }

            for(int i = startIndex; i < endIndex; ++i) {
                if (valueToFind == array[i]) {
                    return i;
                }
            }

            return -1;
        }
    }


    public static int indexOf(byte[] array, byte[] valueToFind) {

        return indexOf(array, valueToFind, -1, -1);

    }

    public static int indexOf(byte[] array, byte[] valueToFind, int startIndex) {

        return indexOf(array, valueToFind, startIndex, -1);

    }

    public static int indexOf(byte[] array, byte[] valueToFind, int startIndex, int endIndex) {

        if (array == null) {
            return -1;
        } else {
            if (startIndex < 0) {
                startIndex = 0;
            }
            if(endIndex > array.length || endIndex < 0){
                endIndex = array.length;
            }
            if(endIndex <= startIndex){
                return -1;
            }

            int index;
            while((index = indexOf(array, valueToFind[0], startIndex, endIndex)) >= 0 && index + valueToFind.length <= endIndex){
                for (int j = 1; j < valueToFind.length; j++) {
                    if(array[index + j] != valueToFind[j]) break;
                }
                return index;
            }
            return -1;
        }

    }


}
